﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Hrm.Core;
using Hrm.EntityFramework;
using Hrm.Services;
using Hrm.Web.Framework;

namespace Hrm.Web.Areas.Hrm.Controllers
{
    [RoleAuthorize]
    public class ClassMatesController : Controller
    {
        IClassMateService _classMateService;
        ISchoolService _schoolService;

        public ClassMatesController(IClassMateService classMateService, ISchoolService schoolService)
        {
            this._classMateService = classMateService;
            this._schoolService = schoolService;
        }

        public ActionResult Index()
        {
            var result = _schoolService.GetAllAbledAndOrderByCreatetime();
            return View(result);
        }

        public JsonResult PageClassMates(PageRequest<ClassMateRequest> request)
        {
            var result = this._classMateService.GetClassMatePage(request);
            return Json(result);
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ClassMate classMate = this._classMateService.GetById(id.Value);
            if (classMate == null)
            {
                return HttpNotFound();
            }
            return View(classMate);
        }

        public ActionResult Create()
        {
            List<SelectListItem> SelectItems = new SelectList(_schoolService.GetAllAbledAndOrderByCreatetime(), "Id", "Name").ToList();
            SelectItems.Insert(0, (new SelectListItem { Text="请选择",Value=""}));
            ViewBag.School_Id = SelectItems;
            
            return View();
        }

        [HttpPost]
        public ActionResult Create(ClassMate classMate, int School_Id)
        {
            if (ModelState.IsValid)
            {
                var school = _schoolService.GetById(School_Id);
                classMate.Schools = new List<School>();
                classMate.Schools.Add(school);
                _classMateService.Insert(classMate);
                return RedirectToAction("Index");
            }
            return View(classMate);
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ClassMate classMate = _classMateService.GetById(id.Value);
            if (classMate == null)
            {
                return HttpNotFound();
            }
            List<SelectListItem> SelectItems = new SelectList(_schoolService.GetAllAbledAndOrderByCreatetime(), "Id", "Name").ToList();
            SelectItems.Single(p => p.Value == classMate.Schools.FirstOrDefault().Id.ToString()).Selected = true;
            SelectItems.Insert(0, (new SelectListItem { Text = "请选择", Value = "" }));
            ViewBag.School_Id = SelectItems;
            return View(classMate);
        }

        [HttpPost]
        public ActionResult Edit(ClassMate classMate, int School_Id)
        {
            if (ModelState.IsValid)
            {
                var school = _schoolService.GetById(School_Id);
                var classMateToUpdate = _classMateService.GetById(classMate.Id);
                classMateToUpdate.Schools.Clear();
                classMateToUpdate.Schools.Add(school);
                if (TryUpdateModel(classMateToUpdate, "", new string[] { "Id", "Name", "Address", "Phone", "QQ", "WeChat", "Company", "Description", "School_Id" }))
                {
                    _classMateService.Update(classMateToUpdate);
                }
                return RedirectToAction("Index");
            }
            return View(classMate);
        }

        [HttpPost]
        public JsonResult Delete(int id)
        {
            ResponseModel result = new ResponseModel();
            try
            {
                ClassMate classMate = _classMateService.GetById(id);
                _classMateService.UpdateDisabled(classMate);
            }
            catch (Exception ex)
            {
                result.SetFailed("删除失败:"+ex.Message);
            }
            return Json(result);
        }
    }
}
